package my.sample.netflow.generator;

import java.net.UnknownHostException;
import java.util.Date;
import java.util.Random;
import java.util.concurrent.Callable;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;

public class NetflowGen implements Callable<Integer>{

	/**
	 * @param args
	 * @throws UnknownHostException 
	 * @throws InterruptedException 
	 */
	
	private int start;
	private int count;
	private MongoClient client;
	String database;
	String collection;
	
	
	public NetflowGen(int start, int count, MongoClient client, String database, String collection) {
		this.start = start;
		this.count = count;
		this.client = client;
		this.database = database;
		this.collection = collection;
	}

	public static void main(String[] args) throws UnknownHostException, InterruptedException {

//		MongoClient client = new MongoClient("localhost", 27011);
/*		List<ServerAddress> seeds = new ArrayList<ServerAddress>();
		seeds.add(new ServerAddress("SS59-MAA", 27021));
		seeds.add(new ServerAddress("SS59-MAA", 27022));
		MongoClient client = new MongoClient(seeds);*/

	}

	@Override
	public Integer call() throws Exception {

		DB db = this.client.getDB(this.database);
		DBCollection collection = db.getCollection(this.collection);
		Random rn = new Random();
		
//		System.out.println("going to insert from : " + this.start + " for : " + this.count);
		
		int endValue = start + count;
		
		for (int j = start; j < endValue; j++) {

			collection.insert(new BasicDBObject()
			.append("routerip", "202.144.74." + rn.nextInt(255))
//			.append("routerid", j)
			.append("sequenceid", j)
			.append("srcadr", "202.144.74." + rn.nextInt(255))
			.append("dstadr", "202.144.74." + rn.nextInt(255))
			.append("inputint", rn.nextInt(5))
			.append("outputint", rn.nextInt(5))
			.append("octets", rn.nextInt(1000))
			.append("srcport", rn.nextInt(25))
			.append("dstport", rn.nextInt(25))
			.append("protocl", 6)
//			.append("tos", "0")
//			.append("dstas", "0")
			.append("direction", rn.nextInt(1))
//			.append("appid", "13")
			.append("timestamp", new Date()));
			
//			Thread.sleep( rn.nextInt(5) * 1000);
//			System.out.println("data inserted : " + new Date().getTime() + " : count : " + j);
			
		}
		
//		System.out.println("count after insert : " + this.start + "-" + this.count +  " = " + collection.count());
		
		client.close();
		
		return null;
	}

}
